<?php


namespace App\Services\Game;


use App\DTOS\Admin\SilentDTO;
use App\Models\Game\Server;
use App\Models\Game\Silent;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class SilentService
{

    public function paginate(SilentDTO $dto){
        $pageSize = $dto->getLimit();
        $page = $dto->getPage();
        $memId = $dto->getMemId();
        $memIds = $dto->getMemIds();
        $roleId = $dto->getRoleId();
        $roleIds = $dto->getRoleIds();
        $model = Silent::query();
        $serverId = $dto->getServerId();
        $model->where('game_area',$serverId);
        if ($memId){
            $model->where('mem_id',$memId);
        }
        if ($roleId){
            $model->where('role_id',$roleId);
        }
        if (!empty($memIds)){
            $model->whereIn('mem_id',$memIds);
        }
        if (!empty($roleIds)){
            $model->whereIn('role_id',$roleIds);
        }
        return $model->orderByDesc('id')->paginate($pageSize,'*','page',$page);
    }

    /**
     * @param array $requestBody 发送的数据包
     * @param int $gameArea 区服ID
     * @return bool
     * @throws \Exception
     */
    private function send($requestBody=[], $gameArea=0){
        $row = Server::query()->where('can_push',1)->where("server_id",$gameArea)->first();
        $pushUrl = data_get($row,'push_url');
        $response = Http::withHeaders(['Content-Type'=>'application/json'])->post($pushUrl.'/Api/Role/BanChat',$requestBody);
        $body = json_decode($response->body(),true);
        $RetCode = data_get($body,'RetCode');
        if ($RetCode!=0) {
            DB::rollBack();
            //  DB::commit();
            $msg = null;
            switch($RetCode){
                case 1: $msg = '失败'; break;
                case 3: $msg = '玩家不在线'; break;
                case 1000: $msg = '无效json参数: ' . json_encode($requestBody); break;
                case 2000: $msg = '操作正在处理'; break;
            }

            throw new \Exception($msg);
        }
        return true;
    }

    public function add(SilentDTO $dto)
    {
        DB::beginTransaction();
        try {
            $roleId = $dto->getRoleId();
            $disableTime = $dto->getDisableTime(); // 禁言时长,秒
            $serverId = $dto->getServerId();
            $data = [];
            $data['game_area'] = $serverId;
            $data['role_id'] = $roleId;
            $data['comment'] = $dto->getComment();
            $data['disable_time'] = $disableTime;
            $data['mem_id'] = $dto->getMemId();
            $data['enable_at'] = date("Y-m-d H:i:s",time() + $disableTime); // 解禁时间
            $data['created_at'] = $data['updated_at'] = now();
            Silent::query()->insert($data);
            $requestBody = [
                'RoleId'=>$roleId,
                'BanTime'=>(int)$disableTime,
            ];
            $this->send($requestBody,$serverId);
            DB::commit();
            return true;
        } catch (\Exception $e){
            DB::rollBack();
            throw $e;
        }
    }

    /**
     * @param SilentDTO $dto ['id','disableTime']
     * @return bool
     * @throws \Exception
     */
    public function edit(SilentDTO $dto)
    {
        DB::beginTransaction();
        try {
            $id = $dto->getId();
            $disableTime = $dto->getDisableTime();
            $detail = Silent::query()->find($id)->toArray();
            $data = [];
            $data['disable_time'] = $disableTime;
            $data['comment'] = $dto->getComment();
            $data['enable_at'] = date("Y-m-d H:i:s",time() + $disableTime); // 解禁时间
            $data['updated_at'] = now();
            Silent::query()->where('id',$id)->update($data);
            $requestBody = [
                'RoleId'=>$detail['role_id'],
                'BanTime'=>(int)$disableTime,
            ];
            $this->send($requestBody,$detail['game_area']);
            DB::commit();
            return true;
        } catch (\Exception $e){
            DB::rollBack();
            throw $e;
        }
    }

}

